package com.biubiux2;

import com.biubiux2.async.DisruptorUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.NettyRuntime;
import lombok.extern.slf4j.Slf4j;

import java.security.NoSuchAlgorithmException;

/**
 * 执行命令：
 * chcp 65001
 *  java '-Dfile.encoding=utf-8' -jar .\server-1.0-SNAPSHOT-jar-with-dependencies.jar
 * @author Lijinbao
 * @since 8/4/2022 6:59 PM
 */
@Slf4j
public class Server {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        run("localhost", 8808);
    }

    @SuppressWarnings("unchecked")
    public static void run(String host, int port) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 256)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .childHandler(new ServerChannelInitializer());

            ChannelFuture channelFuture = b.bind(host, port).sync();
//            DisruptorUtil.init();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DisruptorUtil.shutDown();
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
